package mamikeji.liaowa.ui.activity.article;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mamikeji.liaowa.R;
import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.bean.DayRecommendedArticleBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.Day_recommendAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MultiStateView;
import mamikeji.liaowa.widget.refreshView.PtrlDownRefreshFrameLayout;

public class DayRecommendActivity extends BaseActivity {
    private static final String TAG = "DayRecommendActivity";
    List<DayRecommendedArticleBean.DataEntity> mList = new ArrayList<>();
    private Day_recommendAdapter day_recommendAdapter;
    private PtrlDownRefreshFrameLayout ptrlDownRefreshFrameLayout_dayrecommend;
    private ListView listView_dayrecommend;
    private MultiStateView multiStateView_dayrecommend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_every_day_recommend);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.dayrecommend_name);
        setTitleRightImage(R.drawable.icon_zhuye_n, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyApplication.getInstance().closeActivity(new Article_ListActivity());
                MyApplication.getInstance().closeActivity(new ArticleTypeActivity());
                MyApplication.getInstance().closeActivity(DayRecommendActivity.this);
            }
        });

        ptrlDownRefreshFrameLayout_dayrecommend = (PtrlDownRefreshFrameLayout) findViewById(R.id.ptrlDownRefreshFrameLayout_dayrecommend);
        listView_dayrecommend = (ListView) findViewById(R.id.listView_dayrecommend);
        day_recommendAdapter = new Day_recommendAdapter(mList, this);
        listView_dayrecommend.setAdapter(day_recommendAdapter);

        multiStateView_dayrecommend = (MultiStateView) findViewById(R.id.multiStateView_dayrecommend);

        initdate();

        ptrlDownRefreshFrameLayout_dayrecommend.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView_dayrecommend, header);
            }
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                initdate();
            }
        });
        listView_dayrecommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(DayRecommendActivity.this, Article_DetailsActivity.class);
                intent.putExtra("articleId", mList.get(i).getArticleId() + "");
                intent.putExtra("collectio_article", false);
                startActivity(intent);
            }
        });

        multiStateView_dayrecommend.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateView_dayrecommend.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        initdate();
                    }
                });

        multiStateView_dayrecommend.setEmptyText(R.string.user_collection_article_type).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(DayRecommendActivity.this , ArticleTypeActivity.class));
                finish();
                MyApplication.getInstance().closeActivity(DayRecommendActivity.this);
            }
        });
    }

    /**
     * 获取每日推荐文章
     */
    private void initdate() {

        String url = HttpApi.ROOT_PATH + HttpApi.ARTICLE_day_recommended
                + "?usercode=" + ForPreference.getString(ForContents.USER_CODE);

        ForHttp.getMsg(url, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                multiStateView_dayrecommend.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
                ptrlDownRefreshFrameLayout_dayrecommend.refreshComplete();
            }

            @Override
            public void onResponse(String response) {
                Log.e(TAG, response);
                ptrlDownRefreshFrameLayout_dayrecommend.refreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        DayRecommendedArticleBean articleTypeBean = new Gson().fromJson(response, DayRecommendedArticleBean.class);
                        if (articleTypeBean.getData().size() > 0) {
                            mList = articleTypeBean.getData();
                            day_recommendAdapter.setList(mList);
                            multiStateView_dayrecommend.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            multiStateView_dayrecommend.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    } else {
                        multiStateView_dayrecommend.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
